<template>
  <div>
    <div class="provider-list">
      <div class="header">
        <div class="header-line1 flex-s">
          <el-button type="primary" @click="onOperation({}, 'add')"
            ><el-icon style="margin-right: 2px"><Plus /></el-icon
            >新建联合体</el-button
          >
        </div>
        <div class="header-line2 flex-s">
          <el-input
            v-model="state.params.lianhetimingcheng"
            placeholder="请输入内容"
            style="width: 350px"
            size="large"
          >
            <template #append>
              <el-button
                @click="GetList"
                style="min-width: 50px"
                icon="Search"
              />
            </template>
          </el-input>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.leixing"
            size="large"
            placeholder="联合体类型"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.options"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            >
              {{ item.dicLabel }}
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="content content-table">
        <el-table
          ref="multipleTableRef"
          border
          :data="state.list"
          style="width: calc(100% - 20px)"
        >
          <el-table-column
            property="id"
            label="编号"
            width="100"
            align="center"
            fixed
          />
          <el-table-column
            property="lianhetimingcheng"
            label="联合体名称"
            width="200"
            align="center"
          />
          <el-table-column
            property="zhuliren"
            label="联合体主理人"
            width="150"
            align="center"
          />
          <el-table-column
            property="leixing1"
            label="一级分类"
            width="150"
            align="center"
          />
          <el-table-column
            property="leixing2"
            label="二级分类"
            width="150"
            align="center"
          />
          <el-table-column
            property="canyugongsi"
            label="参与公司数量"
            width="150"
            align="center"
          />
          <el-table-column
            property="fabugongsi"
            label="发布公司名称"
            width="200"
            align="center"
          />
          <el-table-column
            property="beizhu"
            label="备注"
            width="400"
            align="center"
          />
          <el-table-column
            property="shenheTime"
            label="发布时间"
            width="200"
            align="center"
          />
          <el-table-column label="审核状态" width="150" align="center">
            <template #default="scope">
              <span>{{
                scope.row.shenhe?.code == 1
                  ? "审核通过"
                  : scope.row.shenhe?.code == -1
                  ? "审核未通过"
                  : "待审核"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="300"
            align="center"
          >
            <template #default="scope">
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'detail')"
                >详情</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                v-if="scope.row.shenhe?.code == 0 && state.check === '1'"
                @click.prevent="onOperation(scope.row, 'pass')"
                >通过</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                v-if="scope.row.shenhe?.code == 0 && state.check === '1'"
                @click.prevent="onOperation(scope.row, 'deny')"
                >不通过</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'note')"
                >备注</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <h-page
          v-if="state.total"
          v-model:curr-page="state.params.current"
          :size="state.params.size"
          :total="state.total"
          @change="GetList"
        />
      </div>
      <el-dialog
        class="list-dialog"
        show-close="false"
        width="40%"
        v-model="state.show"
        center
        style="padding: 0 30px"
      >
        <el-form :model="state.form1">
          <el-form-item label="备注：">
            <el-input
              v-model="state.form1.beizhu"
              :maxlength="500"
              type="textarea"
              :rows="8"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-form-item class="note-button-group">
          <el-button size="large" @click="state.show = false">取消</el-button>
          <el-button
            class="ml50"
            size="large"
            type="primary"
            @click="submitForm"
          >
            提交
          </el-button>
        </el-form-item>
      </el-dialog>
      <el-dialog
        class="list-dialog"
        show-close="false"
        width="40%"
        v-model="state.showReview"
        center
        style="padding: 0 30px"
      >
        <el-form :model="state.reviewForm">
          <el-form-item label="不通过原因：">
            <el-input
              v-model="state.reviewForm.shenheBeizhu"
              :maxlength="150"
              type="textarea"
              :rows="8"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-form-item class="note-button-group">
          <el-button size="large" @click="state.showReview = false"
            >取消</el-button
          >
          <el-button class="ml50" size="large" type="primary" @click="review">
            提交
          </el-button>
        </el-form-item>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import { reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  GetConsortiumList,
  NoteConsortium,
  ReviewConsortium,
} from "@/api/consortium";
import { baseMsg } from "@/utils/utils";
import { GetDictionary } from "@/api/system";
const route = useRoute();
const router = useRouter();
const state = reactive({
  params: {
    current: Number(route.query.page) || 1,
    size: Number(route.query.size) || 10,
    lianhetimingcheng: "",
    leixing: "",
  },
  show: false,
  form1: {
    id: "",
    beizhu: "",
  },
  // 审核表单
  reviewForm: {
    id: null,
    shenheBeizhu: "",
    shenhe: null,
    lhtNo: "",
  },
  showReview: false,
  list: [],
  options: [],
  total: 0,
  check: localStorage.getItem("check"),
});
const GetList = () => {
  GetConsortiumList(state.params).then((res) => {
    if (res.code == 4201) {
      state.list = res.data.records;
      state.total = res.data.total;
    }
  });
};
// 备注提交
const submitForm = async () => {
  const res = await NoteConsortium(state.form1);
  if (res.code == 4202) {
    baseMsg("备注成功");
    GetList();
  }
  state.show = false;
};
// 联合体类型下拉
const GetConsortiumType = () => {
  GetDictionary({ dicId: "lianhetileixing" }).then((res) => {
    if (res.code == 4201) {
      state.options = res.data;
    }
  });
};
// 操作
const onOperation = (params, operation) => {
  if (operation === "detail") {
    router.push({
      path: `/lianheti/detail`,
      query: {
        id: params.id,
      },
    });
  }
  if (operation === "add" || operation === "edit") {
    router.push({
      path: `/lianheti/info`,
      query: {
        id: params.id || null,
        type: operation,
      },
    });
  }
  if (operation === "note") {
    state.show = true;
    state.form1.id = params.id;
    state.form1.beizhu = params.beizhu;
  }
  if (operation === "deny" || operation === "pass") {
    state.showReview = false;
    state.reviewForm.id = params.id;
    state.reviewForm.shenheBeizhu = "";
    state.reviewForm.lhtNo = params.lhtNo;
    if (operation === "deny") {
      state.showReview = true;
      state.reviewForm.shenhe = -1;
    } else {
      state.reviewForm.shenhe = 1;
      review();
    }
  }
};
// 审核
const review = () => {
  ReviewConsortium(state.reviewForm).then((res) => {
    if (res.code == 4202) {
      baseMsg("操作成功");
      GetList();
    }
    state.showReview = false;
  });
};
watch(
  () => route.query,
  () => {
    state.params.current = Number(route.query.page) || 1;
    GetList();
  },
  { deep: true }
);
GetList();
GetConsortiumType();
</script>
<style lang="scss" scoped>
.header {
  border-bottom: 10px solid #f2f2f2;
  border-radius: 10px 10px 0 0;
}
.header-line1 {
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  border-radius: 10px 10px 0 0;
}
.header-line2 {
  padding: 20px 10px;
}
.content {
  border: none !important;
  margin: 20px 10px;
  background-color: transparent !important;
  padding-bottom: 30px;
}
</style>
<style lang="scss">
.content-table .el-table__header th {
  background-color: #f2f9fc !important;
}
.note-button-group {
  .el-form-item__content {
    justify-content: center;
  }
}
</style>
